# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [u'TraitUi']

from Qt4 import QtGui
from traits.api import Unicode, Int, Bool
from _pylot.qtgui.tools import (set_widget_value,
                              get_widget_value,
                              get_widget_prefix,
                              connect_widget_value_changed)

class TraitUi(QtGui.QWidget):


  selectors = {
    # TraitType : (WidgetClass, option dict)

    Unicode:(QtGui.QLineEdit, {}),
    Int:(QtGui.QSpinBox, {}),
    Bool:(QtGui.QCheckBox, {}),

  }

  def __init__(self, trait):
    QtGui.QWidget.__init__(self)
    self.trait = trait

    try :
      label = trait.label
    except AttributeError :
      label = trait.__class__.__name__

    self.setObjectName(label)
    self.setWindowTitle(label)

    self.layout = QtGui.QFormLayout(self)
    self.layout.setMargin(0)
    self.layout.setObjectName("layout")

    self.widget2trait = {}
    self.trait2widget = {}

    datas = trait.get()
    cpt = 0
    keys = datas.keys()
    keys.sort()
    for t_name in keys :
      value = datas[t_name]
      t = trait.traits()[t_name]
      if t.label :
        label = t.label
      else :
        label = t_name
      label = QtGui.QLabel(label)
      label.setObjectName('l_%s' % t_name)

#        values = list(t.trait_type.values)
#        values.insert(0, values.index(value))
      if type(t.trait_type) in self.selectors :
        selector, kargs = self.selectors[type(t.trait_type)]
        selector = selector(**kargs)
      else :
        continue

      prefix = get_widget_prefix(selector)
      selector.setObjectName('%s%s' % (prefix, t_name))

      self.widget2trait[selector] = t_name
      self.trait2widget[t_name] = selector

      # equivalent to w.setValue(value), w.setText(value), ...
      set_widget_value(selector, value)


      # equivalent to w.calueChanged.connect(self.method)
      connect_widget_value_changed(selector, self.valueChanged)
      self.trait.on_trait_change(self.refresh, t_name)

      self.layout.addRow(label, selector)
      cpt += 1

    if not cpt :
      self.layout.addRow(QtGui.QLabel(u'********'),
      QtGui.QLabel(u'Aucun selecteur trouvé'))

  def valueChanged(self, *args):
    sender = self.sender()
    t_name = self.widget2trait[sender]
    setattr(self.trait, t_name, get_widget_value(sender))

  def refresh(self, trait, t_name, value):
    # Available signatures for trait changed event :
    #  - refresh(self, value)
    #  - refresh(self, t_name, value)
    #  - refresh(self, trait, t_name, value)        <-- CURRENTLY USED SIGNATURE
    #  - refresh(self, trait, t_name, old_value, value)
    set_widget_value(self.trait2widget[t_name], value)



